﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Common;
using DAL;
using Model;
namespace UI.member
{
    public partial class index : System.Web.UI.Page
    {
        WeiXin wx = new WeiXin();
        public string signature = "";
        public string noncestr = "";
        public long timestamp = 0;
        public UserInfo uinfo = new UserInfo();
        public ConfigInfo config = new ConfigInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            B.MyUser = null;
            if (B.MyUser != null && B.MyUser.ID == 16779)
            {
                B.MyUser = null;
            }
            if (Request["code"] == null && B.MyUser == null)
            {
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a4dea3fd54864b9&redirect_uri=" + base.Server.UrlEncode("http://www.mengshen666.cn/member/index.aspx") + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
            }
            else
            {
                config = ConfigDAL.GetConfig();
                string token = config.Token;
                string ticket = config.Ticket;
                noncestr = Util.CreateNonce_str();
                timestamp = Util.CreateTimestamp();
                signature = GetSignature(ticket, noncestr, timestamp, Request.Url.ToString());
                
                if (B.MyUser == null)
                {
                    string result = wx.HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx1a4dea3fd54864b9&secret=75fac3337cd918fd762b48f368cba18e&code=" + Request["code"] + "&grant_type=authorization_code", "");
                    string str = wx.HttpGet("https://api.weixin.qq.com/sns/userinfo?access_token=" + wx.ConvertJson(result).access_token + "&openid=" + wx.ConvertJson(result).openid + "&lang=zh_CN", "");
                    dynamic user = wx.ConvertJson(str);
                    /*
                    UserInfo bak = UserDAL.FindByHeadImg(user.province, user.city, user.nickname, user.sex);
                    if (bak.ID > 0)
                    {
                        UserDAL.UpdateOpenID(user.openid, user.province, user.city, user.nickname, user.sex);
                    }
                    */
                    uinfo = UserDAL.FindByOpenID(user.openid);
                    if (uinfo.ID == 0)
                    {
                        uinfo.OpenID = user.openid;
                        uinfo.NickName = user.nickname;
                        uinfo.HeadImg = user.headimgurl;
                        uinfo.Mobile = "";
                        uinfo.UserPwd = "";
                        uinfo.Province = user.province;
                        uinfo.City = user.city;
                        uinfo.Area = "";
                        uinfo.Sex = user.sex;
                        uinfo.Money = 0;
                        uinfo.Role = 1;
                        uinfo.State = -1;
                        uinfo.EndDate = DateTime.Now;
                        uinfo.CreateDate = DateTime.Now;
                        int ID = UserDAL.Save(uinfo);
                        uinfo.ID = ID;
                        B.MyUser = uinfo;
                    }
                    else
                    {
                        B.MyUser = uinfo;
                    }
                }
                else
                {
                    if (B.MyUser.ID > 0)
                    {
                        uinfo = UserDAL.FindOneByID(B.MyUser.ID);
                    }
                    else
                    {
                        B.MyUser = null;
                        Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a4dea3fd54864b9&redirect_uri=" + base.Server.UrlEncode("http://www.mengshen666.cn/member/index.aspx") + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
                    }
                }
                if (uinfo.EndDate < DateTime.Now)
                {
                    config.ShouXuFei = config.ShouXuFei2;
                    
                }
            }
        }
        public static string GetSignature(string jsapi_ticket, string noncestr, long timestamp, string url)
        {
            var string1Builder = new StringBuilder();
            string1Builder.Append("jsapi_ticket=").Append(jsapi_ticket).Append("&")
                          .Append("noncestr=").Append(noncestr).Append("&")
                          .Append("timestamp=").Append(timestamp).Append("&")
                          .Append("url=").Append(url.IndexOf("#") >= 0 ? url.Substring(0, url.IndexOf("#")) : url);
            string string1 = string1Builder.ToString();
            return Util.Sha1(string1);
        }
    }

}